using System;
using System.Web;
using FwkLight.CodeExtensions;

namespace FwkLight.Web
{
  public interface ISessionItemDictionary : IItemDictionary
  {
    void Remove<Item> ( IItemKey<Item> key );
  }

  public class SessionItemDictionary : ISessionItemDictionary
  {
    public void Add<ItemToPutInContext> ( IItemKey<ItemToPutInContext> key, ItemToPutInContext item )
    {
      if (HttpContext.Current == null || HttpContext.Current.Session == null)
        return;

      HttpContext.Current.Session[key.Key] = item;
    }

    public Item Get<Item> ( IItemKey<Item> key )
    {
      if (HttpContext.Current == null || HttpContext.Current.Session == null || !Contains(key))
        return default(Item);

      return (Item)HttpContext.Current.Session[key.Key];
    }

    public bool Contains<Item> ( IItemKey<Item> key )
    {
      if (HttpContext.Current == null || HttpContext.Current.Session == null)
        return false;

      return (HttpContext.Current.Session[key.Key] != null);
    }

    public void Remove<ItemToPutIn> ( ItemKey<ItemToPutIn> key )
    {
      throw new NotImplementedException();
    }

    public void Remove<Item> ( IItemKey<Item> key )
    {
      if (HttpContext.Current == null || HttpContext.Current.Session == null)
        return;

      HttpContext.Current.Session.Remove(key.Key);
    }
  }
}